local bugui = fk.CreateSkill{
  name = "wk_heg__bugui",
}

Fk:loadTranslationTable{
  ["wk_heg__bugui"] = "卜规",
  [":wk_heg__bugui"] = "出牌阶段结束时，你可以判定至多三次，然后若本回合进入弃牌堆牌的花色数因此：增加，你执行一个额外的出牌阶段；达到四种，你摸两张牌或<a href=':ElectedChange'>推举</a>。",

  ["#wk_heg__bugui_continue"] = "卜规：你可以继续判定",
  ["wk_heg__bugui_draw"] = "摸两张牌",
  ["wk_heg__bugui_elect"] = "推举",

  ["$wk_heg__bugui1"] = "论演玄意，以筮辄验。",
  ["$wk_heg__bugui2"] = "手不释书，好研经卷。",
}

local HG = require "packages.new_heg_diy_my.util"

bugui:addEffect(fk.EventPhaseEnd, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(bugui.name) and player == target and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    
    local turn_event_old = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
    if turn_event_old == nil then return false end
    local end_id_old = turn_event_old.id
    local cards_old = {}

    room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local card_suit = Fk:getCardById(info.cardId, true).suit
            if room:getCardArea(info.cardId) == Card.DiscardPile and not table.contains(cards_old, card_suit) and card_suit ~= 0 then
              table.insertIfNeed(cards_old, card_suit)
            end
          end
        end
      end
      return false
    end, end_id_old)

    local i = 0
    while i < 3 do
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".",
      }
      room:judge(judge)
      i = i + 1
      if i >= 3 or not room:askToSkillInvoke(player, {skill_name = bugui.name, prompt = "#wk_heg__bugui_continue" }) then
        break
      end
    end
    
    -- local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
    -- if turn_event == nil then return false end
    -- local end_id = turn_event.id
    local cards = {}
    room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local card_suit = Fk:getCardById(info.cardId, true).suit
            if room:getCardArea(info.cardId) == Card.DiscardPile and not table.contains(cards, card_suit) and card_suit ~= 0 then
              table.insertIfNeed(cards, card_suit)
            end
          end
        end
      end
      return false
    end, end_id_old)

    if #cards == 4 and #cards_old < 4 then
      local choices = {"wk_heg__bugui_draw", "wk_heg__bugui_elect"}
      local choice = room:askToChoice(player, {skill_name = bugui.name, choices = choices})
      if choice == "wk_heg__bugui_draw" then
        player:drawCards(2, bugui.name)
      else
        HG.DoElectedChange(room, player, bugui.name)
      end
    end
    if #cards > #cards_old and #cards <= 4 then
      player:gainAnExtraPhase(Player.Play)
    end
  end,
})

return bugui